package com.souku.finance.application.ui.more;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.ActivityManage;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.ViewUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.HashMap;

/**
 * Created by Android on 2016/12/7.
 */

public class ForgetLoginPassActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected TextView tvDec;
    protected EditText etPass;
    protected Button btnCode;
    protected ImageView ivDelet;
    protected Button btnOk;
    protected CheckBox checkbox;
    protected TextView tvShow;
    protected LinearLayout llShow;
    protected ImageView image;
    private int step = 0;
    private boolean isSend;
    private String phone = "";
    private String codeToken = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_forgetloginpass);
        initView();
        ViewUtil.setCountDown(btnCode, this);
        ViewUtil.setDelete(etPass, ivDelet, null);
        topTitle.setText("忘记登录密码");
        setstep();
    }

    @Override
    public void internet() {

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            if (step == 0) {
                finish();
            } else {
                step--;
                setstep();
            }
        } else if (view.getId() == R.id.btn_code) {
            sendCode(phone);
        } else if (view.getId() == R.id.ll_show) {
            checkbox.setChecked(!checkbox.isChecked());
            if (!checkbox.isChecked()) {
                etPass.setInputType(129);
            } else {
                etPass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            etPass.setSelection(etPass.getText().toString().trim().length());
        } else if (view.getId() == R.id.btn_ok) {
            if (step == 0) {
                String s = etPass.getText().toString().trim();
                if (MyTextUtil.isEmpty(s)) {
                    ToastUtil.show("请输入手机号");
                    return;
                }
                if (!MyTextUtil.isTelephone(s)) {
                    ToastUtil.show("请输入正确的手机号");
                    return;
                }
                btnOk.setEnabled(false);
                isRegister(s);
            } else if (step == 1) {
                String s = etPass.getText().toString().trim();
                if (MyTextUtil.isEmpty(s)) {
                    ToastUtil.show("请输入验证码");
                    return;
                }
                btnOk.setEnabled(false);
                checkCode(phone, s);
            } else if (step == 2) {
                String s = etPass.getText().toString().trim();
                if (MyTextUtil.isEmpty(s)) {
                    ToastUtil.show("请输入密码");
                    return;
                }
                if (s.length() < 8) {
                    ToastUtil.show("密码长度8-20位");
                    return;
                }
                set_new_password(s);
            }
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(ForgetLoginPassActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        tvDec = (TextView) findViewById(R.id.tv_dec);
        etPass = (EditText) findViewById(R.id.et_pass);
        btnCode = (Button) findViewById(R.id.btn_code);
        btnCode.setOnClickListener(ForgetLoginPassActivity.this);
        ivDelet = (ImageView) findViewById(R.id.iv_delet);
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(ForgetLoginPassActivity.this);
        checkbox = (CheckBox) findViewById(R.id.checkbox);
        tvShow = (TextView) findViewById(R.id.tv_show);
        llShow = (LinearLayout) findViewById(R.id.ll_show);
        llShow.setOnClickListener(ForgetLoginPassActivity.this);
        image = (ImageView) findViewById(R.id.image);
    }

    private void setstep() {
        etPass.setText("");
        switch (step) {
            case 0:
                etPass.setText(phone);
                tvDec.setText("输入你的手机号");
                btnCode.setVisibility(View.GONE);
                llShow.setVisibility(View.GONE);
                image.setVisibility(View.VISIBLE);
                etPass.setHint("手机号");
                image.setImageResource(R.drawable.icon_phone);
                etPass.setInputType(InputType.TYPE_CLASS_NUMBER);
                btnOk.setText("下一步");
                break;
            case 1:
                if (isSend) {
                    tvDec.setText("短信码已经发送至你的手机");
                } else {
                    tvDec.setText("点击立即获取验证码");
                }
                btnCode.setVisibility(View.VISIBLE);
                llShow.setVisibility(View.GONE);
                image.setVisibility(View.VISIBLE);
                etPass.setHint("输入验证码");
                image.setImageResource(R.drawable.icon_yanzheng);
                etPass.setInputType(InputType.TYPE_CLASS_NUMBER);
                btnOk.setText("下一步");
                break;
            case 2:
                tvDec.setText("重置登录密码");
                btnCode.setVisibility(View.GONE);
                llShow.setVisibility(View.VISIBLE);
                image.setVisibility(View.GONE);
                etPass.setHint("输入8-20位登录密码");
                etPass.setInputType(InputType.TYPE_CLASS_TEXT);
                btnOk.setText("完成");
                break;
        }
    }

    private void isRegister(final String mobile) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        RequestManager.getInstance().post(UrlConstact.IS_REGISTER, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                btnOk.setEnabled(true);
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("手机号未注册");
                } else {
                    phone = mobile;
                    step++;
                    setstep();
                }
            }

            @Override
            public void onError(String errorMsg) {
                btnOk.setEnabled(true);
            }
        }), ParamUtil.getHeaders());
    }

    //发送验证码
    private void sendCode(String mobile) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        RequestManager.getInstance().post(UrlConstact.SEND_MOBILE_CODE, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                btnOk.setEnabled(true);
                if (JsonUtil.getCode(response) == 1) {
                    tvDec.setText("短信码已经发送至你的手机");
                    isSend = true;
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                btnOk.setEnabled(true);
            }
        }), ParamUtil.getHeaders());
    }


    //检验验证码
    private void checkCode(final String mobile, String code) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        map.put("code", code);
        RequestManager.getInstance().post(UrlConstact.VALIDATE_MOBILE_CODE, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                btnOk.setEnabled(true);
                if (JsonUtil.getCode(response) == 1) {
                    step++;
                    codeToken = JsonUtil.getString(JsonUtil.getData(response), "verifyToken");
                    setstep();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                btnOk.setEnabled(true);
            }
        }), ParamUtil.getHeaders());
    }

    //设置新密码
    private void set_new_password(final String pwd) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", phone);
        map.put("verifyToken", codeToken);
        map.put("userPwd", pwd);
        RequestManager.getInstance().post(UrlConstact.MODIFY_PASSWORD, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                ToastUtil.show(JsonUtil.getMessage(response));
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("修改成功");
                    ActivityManage.finishSame(ResetLoginPassActivity.class);
                    finish();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }
}
